MOOC Utils

Utils like auto assignment solver extension and course notice announcment notification for MOOC courses

Answer application handler

Introduction#

Applies AI-generated answers to form controls on assignment pages. Covers question types, answer shape checks, DOM writes, synthetic user events, errors, and Chrome vs Firefox differences.

Project structure#

The Answer Application Handler resides in the content script layer of the assignment-solver extension. It communicates with the background script via message passing and interacts directly with the page DOM to apply answers and submit assignments.

Core components#

  • Answer Applicator: Applies answers to single-choice, multiple-choice, and fill-in-the-blank form elements. It simulates user interactions by triggering click events and input/change events.
  • Content Script Entry: Receives messages from the background script, initializes services, and forwards commands to the applicator.
  • Answer Handler: Manages content script injection, message routing, and error handling for answer application and submission.
  • Message System: Defines standardized message types for communication between UI, background, and content scripts.
  • Cross-Browser Compatibility: Uses webextension-polyfill for unified browser APIs and includes Firefox-specific initialization delays.

Architecture overview#

The handler follows a message-driven architecture:

  • UI controllers trigger actions (solve, apply answers, submit).
  • Background answer handler checks that the content script is loaded, then forwards messages.
  • Content script applies answers to the DOM and triggers submission.

Detailed component analysis#

Answer applicator#

The applicator encapsulates three primary operations. single-choice selection, multi-choice selection, and fill-in-the-blank text input. It validates inputs, performs reliable DOM queries, and simulates realistic user interactions.

  • Supported question types:

  • Single choice (radio button)

  • Multi choice (checkbox)

  • Fill in the blank (input/textarea)

  • Answer format validation:

  • Single choice requires answer_option_id.

  • Multi choice accepts a single ID or an array of IDs.

  • Fill in the blank requires answer_text and optionally answer_option_id for targeting.

  • DOM manipulation techniques:

  • Radio buttons: click() followed by change event dispatch.

  • Checkboxes: toggle click() and change event dispatch per option.

  • Text inputs: set value, dispatch input, change, and keyup events.

  • Dynamic element detection:

  • Attempts lookup by ID, value attribute, name containing question ID, and partial ID matches.

  • For fill blanks, searches across input and textarea elements within question containers.

  • User interaction simulation:

  • Dispatches synthetic events to trigger change handlers and validation logic.

  • Ensures UI reflects selections immediately.

Content script entry#

The content script initializes logging, creates extractor and applicator instances, and listens for messages from the background script. It supports health checks, page extraction, scrolling, and answer application/submission.

  • Message handling:

  • PING: responds to health checks.

  • GET_PAGE_HTML: extracts page HTML and images.

  • GET_PAGE_INFO: quick assignment detection metadata.

  • APPLY_ANSWERS: delegates to applicator.

  • SUBMIT_ASSIGNMENT: triggers submission with fallback selectors.

  • Cross-browser compatibility:

  • Uses webextension-polyfill for unified browser APIs.

  • Includes Firefox-specific initialization delay in the answer handler.

Answer handler (background)#

The background answer handler manages content script lifecycle and message routing. It checks that the content script is loaded, injects if missing, and forwards messages with error handling.

  • Content script injection:

  • Sends PING to verify readiness.

  • Executes content script if missing.

  • Applies extended delay for Firefox initialization.

  • Error handling:

  • Catches injection failures and instructs users to refresh the page.

  • Forwards errors from content script responses.

Message types and validation#

Standardized message types define the contract between UI, background, and content scripts. The applicator validates answer arrays and individual answer objects before applying.

  • Message types:

  • PING, GET_PAGE_HTML, GET_PAGE_INFO, APPLY_ANSWERS, SUBMIT_ASSIGNMENT.

  • Answer validation:

  • applyAnswers checks for array presence and iterates with per-answer error handling.

  • Single/multi/fill handlers validate required fields and log missing data.

Cross-Browser compatibility#

The extension targets both Chrome and Firefox using webextension-polyfill. The answer handler includes a deliberate delay for Firefox so the content script initializes before sending messages.

  • Browser detection:

  • detectBrowser() identifies Firefox vs Chrome.

  • isFirefox()/isChrome() helpers for conditional logic.

  • Initialization differences:

  • Firefox receives a longer wait after content script injection.

Dependency analysis#

The applicator depends on the content script environment and browser APIs. The content script depends on the applicator and logger. The background answer handler depends on tabs and scripting adapters and coordinates with the content script.

Performance considerations#

  • DOM queries are optimized with targeted selectors and fallbacks to reduce unnecessary traversal.
  • Synthetic events are dispatched minimally and only when elements are found.
  • Multi-choice processing iterates through discovered checkboxes and toggles only when needed.
  • Logging is used to avoid repeated expensive operations and to surface issues early.

Troubleshooting guide#

Common issues and resolutions:

  • Content script not loaded:
  • The answer handler attempts injection and verifies with PING. If still failing, instruct users to refresh the page.
  • Answers not applied:
  • Verify answer arrays are present and properly formatted. Check logs for unknown question types or missing IDs.
  • Submission fails:
  • Confirm submit button selectors are matched. The applicator tries multiple fallbacks if the explicit ID is unavailable.
  • Cross-browser delays:
  • Firefox may require additional initialization time. The handler includes a delay to mitigate timing issues.

Conclusion#

Validate the answer payload, find the control, simulate input, and surface failures per question instead of failing the whole batch silently.